home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dvips.new / resident.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-15  |  18.5 KB  |  742 lines

  1. /*
  2.  *   This code reads in and handles the defaults for the program from the
  3.  *   file config.sw.  This entire file is a bit kludgy, sorry.
  4.  */
  5. #include "structures.h" /* The copyright notice in that file is included too! */
  6. #include "paths.h"
  7. /*
  8.  *   This is the structure definition for resident fonts.  We use
  9.  *   a small and simple hash table to handle these.  We don't need
  10.  *   a big hash table.
  11.  */
  12. struct resfont *reshash[RESHASHPRIME] ;
  13. /*
  14.  *   These are the external routines we use.
  15.  */
  16. extern void error() ;
  17. extern integer scalewidth() ;
  18. extern void tfmload() ;
  19. extern FILE *search() ;
  20. extern shalfword pkbyte() ;
  21. extern integer pkquad() ;
  22. extern integer pktrio() ;
  23. extern Boolean pkopen() ;
  24. extern char *strcpy() ;
  25. extern char *getenv() ;
  26. extern char *newstring() ;
  27. extern int add_header() ;
  28. extern int add_name() ;
  29. extern char *get_name() ;
  30. extern int system() ;
  31. /*
  32.  *   These are the external variables we use.
  33.  */
  34. #ifdef DEBUG
  35. extern integer debug_flag;
  36. #endif  /* DEBUG */
  37. extern long bytesleft ;
  38. extern quarterword *raster ;
  39. extern FILE *pkfile ;
  40. extern char *oname ;
  41. extern integer swmem, fontmem ;
  42. extern char *tfmpath ;
  43. extern char *pkpath ;
  44. extern char *vfpath ;
  45. extern char *figpath ;
  46. extern char *configpath ;
  47. #ifdef SEARCH_SUBDIRECTORIES
  48. extern char *fontsubdirpath ;
  49. #endif
  50. #ifdef FONTLIB
  51. extern char *flipath, *fliname ;
  52. #endif
  53. extern char *headerpath ;
  54. extern char *paperfmt ; 
  55. extern char *nextstring ;
  56. extern char *maxstring ;
  57. extern char *warningmsg ;
  58. extern Boolean disablecomments ;
  59. extern Boolean compressed ;
  60. extern int quiet ;
  61. extern int filter ;
  62. extern Boolean reverse ;
  63. extern Boolean usesPSfonts ;
  64. extern Boolean nosmallchars ;
  65. extern Boolean removecomments ;
  66. extern Boolean safetyenclose ;
  67. extern int actualdpi ;
  68. extern int vactualdpi ;
  69. extern int maxdrift ;
  70. extern int vmaxdrift ;
  71. extern char *printer ;
  72. extern char *mfmode ;
  73. extern int lastresortsizes[] ;
  74. /*
  75.  *   To maintain a list of document fonts, we use the following
  76.  *   pointer.
  77.  */
  78. struct header_list *ps_fonts_used ;
  79. /*
  80.  *   We use malloc here.
  81.  */
  82. char *malloc() ;
  83. /*
  84.  *   Our hash routine.
  85.  */
  86. int
  87. hash(s)
  88.    char *s ;
  89. {
  90.    int h = 12 ;
  91.  
  92.    while (*s != 0)
  93.       h = (h + h + *s++) % RESHASHPRIME ;
  94.    return(h) ;
  95. }
  96. /*
  97.  *   cleanres() marks all resident fonts as not being yet sent.
  98.  */
  99. void
  100. cleanres() {
  101.    register int i ;
  102.    register struct resfont *p ;
  103.    for (i=0; i<RESHASHPRIME; i++)
  104.       for (p=reshash[i]; p; p=p->next)
  105.          p->sent = 0 ;
  106. }
  107. /*
  108.  *   The routine that looks up a font name.
  109.  */
  110. struct resfont *
  111. lookup(name)
  112.    char *name ;
  113. {
  114.    struct resfont *p ;
  115.  
  116.    for (p=reshash[hash(name)]; p!=NULL; p=p->next)
  117.       if (strcmp(p->Keyname, name)==0)
  118.          return(p) ;
  119.    return(NULL) ;
  120. }
  121. /*
  122.  *   This routine adds an entry.
  123.  */
  124. void
  125. add_entry(Keyname, TeXname, PSname, specinfo, downloadinfo)
  126.    char *Keyname, *TeXname, *PSname, *specinfo, *downloadinfo ;
  127. {
  128.    struct resfont *p ;
  129.    int h ;
  130.  
  131.    if (PSname == NULL)
  132.       PSname = TeXname ;
  133.    else if (strcmp(PSname, TeXname) && Keyname != PSname)
  134.       add_entry(PSname, TeXname, PSname, specinfo, downloadinfo) ;
  135.    p = (struct resfont *)malloc((unsigned int)sizeof(struct resfont)) ;
  136.    if (p==NULL)
  137.       error("! out of memory") ;
  138.    p->Keyname = Keyname ;
  139.    p->PSname = PSname ;
  140.    p->TeXname = TeXname ;
  141.    p->specialinstructions = specinfo ;
  142.    p->downloadheader = downloadinfo ;
  143.    h = hash(Keyname) ;
  144.    p->next = reshash[h] ;
  145.    p->sent = 0 ;
  146.    reshash[h] = p ;
  147. }
  148. /*
  149.  *   Now our residentfont routine.
  150.  */
  151. Boolean
  152. residentfont(curfnt)
  153.         register fontdesctype *curfnt ;
  154. {
  155.    register shalfword i ;
  156.    struct resfont *p ;
  157.  
  158. /*
  159.  *   First we determine if we can find this font in the resident list.
  160.  */
  161.    if (*curfnt->area)
  162.       return 0 ; /* resident fonts never have a nonstandard font area */
  163.    if ((p=lookup(curfnt->name))==NULL)
  164.       return 0 ;
  165. /*
  166.  *   We clear out some pointers:
  167.  */
  168. #ifdef DEBUG
  169.    if (dd(D_FONTS))
  170.         (void)fprintf(stderr,"Font %s <%s> is resident.\n",
  171.                                      curfnt->name, p->PSname) ;
  172. #endif  /* DEBUG */
  173.    curfnt->resfont = p ;
  174.    curfnt->name = p->TeXname ;
  175.    for (i=0; i<256; i++) {
  176.       curfnt->chardesc[i].TFMwidth = 0 ;
  177.       curfnt->chardesc[i].packptr = NULL ;
  178.       curfnt->chardesc[i].pixelwidth = 0 ;
  179.       curfnt->chardesc[i].flags = 0 ;
  180.    }
  181.    add_name(p->PSname, &ps_fonts_used) ;
  182. /*
  183.  *   We include the font here.  But we only should need to include the
  184.  *   font if we have a stupid spooler; smart spoolers should be able
  185.  *   to supply it automatically.
  186.  */
  187.    if (p->downloadheader)
  188.       if (add_header(p->downloadheader)) {
  189.          swmem -= DNFONTCOST ;
  190.          fontmem -= DNFONTCOST ;
  191.       }
  192.    tfmload(curfnt) ;
  193.    usesPSfonts = 1 ;
  194.    return(1) ;
  195. }
  196. #define INLINE_SIZE (500)
  197. static char was_inline[INLINE_SIZE] ;
  198. void
  199. bad_config() {
  200.    extern void exit() ;
  201.  
  202.    error("Error in config file:") ;
  203.    (void)fputs(was_inline, stderr) ;
  204.    exit(1) ;
  205. }
  206. /*
  207.  *   Now we have the getdefaults routine.
  208.  */
  209. static char *psmapfile = PSMAPFILE ;
  210. void
  211. getdefaults(s)
  212. char *s ;
  213. {
  214.    FILE *deffile ;
  215.    char PSname[300] ;
  216.    register char *p ;
  217.    int i, j ;
  218.    char *d = configpath ;
  219.  
  220.    if (printer == NULL) {
  221.       if (s) {
  222.          strcpy(PSname, s) ;
  223.       } else {
  224.          d = "~" ;
  225.          strcpy(PSname, DVIPSRC) ;
  226.       }
  227.    } else {
  228.       strcpy(PSname, "config.") ;
  229.       strcat(PSname, printer) ;
  230.    }
  231.    if ((deffile=search(d,PSname,READ))!=NULL) {
  232.       while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  233. /*
  234.  *   We need to get rid of the newline.
  235.  */
  236.        for (p=was_inline; *p; p++) ;
  237.        if (p > was_inline) *(p-1) = 0 ;
  238.        switch (was_inline[0]) {
  239. case 'm' :
  240. #ifdef SHORTINT
  241.          if (sscanf(was_inline+1, "%ld", &swmem) != 1) bad_config() ;
  242. #else   /* ~SHORTINT */
  243.          if (sscanf(was_inline+1, "%d", &swmem) != 1) bad_config() ;
  244. #endif  /* ~SHORTINT */
  245.          break ;
  246. case 'M' :
  247.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  248.          mfmode = newstring(PSname) ;
  249.          break ;
  250. case 'o' : case 'O' :
  251.          p = was_inline + 1 ;
  252. #ifdef VMS
  253.      p[strlen(p) - 1] = '\0';
  254. #endif /* VMS */
  255.          while (*p && *p <= ' ')
  256.             p++ ;
  257.          oname = newstring(p) ;
  258.          break ;
  259. #ifdef FONTLIB
  260. case 'L' : 
  261.          {
  262.             char tempname[300] ;
  263.             extern char *fliparse() ;
  264.             if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  265.             else {
  266.                flipath = newstring(fliparse(PSname,tempname));
  267.                fliname = newstring(tempname) ;
  268.             }
  269.      }
  270.          break ;
  271. #endif
  272. case 'T' : 
  273.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  274.          else tfmpath = newstring(PSname) ;
  275.          break ;
  276. case 'P' :
  277.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  278.          else pkpath = newstring(PSname) ;
  279.          break ;
  280. case 'p' :
  281.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  282.          else psmapfile = newstring(PSname) ;
  283.          break ;
  284. case 'V' : case 'v' :
  285.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  286.          else vfpath = newstring(PSname) ;
  287.          break ;
  288. case 'S' :
  289.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  290.          else figpath = newstring(PSname) ;
  291.          break ;
  292. case 's':
  293.          safetyenclose = 1 ;
  294.          break ;
  295. case 'H' : 
  296.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  297.          else headerpath = newstring(PSname) ;
  298.          break ;
  299. case 't' : 
  300.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  301.          else paperfmt = newstring(PSname) ;
  302.          break ;
  303. case ' ' : case '*' : case '#' : case ';' : case '=' : case 0 : case '\n' :
  304.          break ;
  305. case 'r' :
  306.          reverse = (was_inline[1] != '0') ;
  307.          break ;
  308. /*
  309.  *   This case is for last resort font scaling; I hate this, but enough
  310.  *   people have in no uncertain terms demanded it that I'll go ahead and
  311.  *   add it.
  312.  *
  313.  *   This line must have numbers on it, resolutions, to search for the
  314.  *   font as a last resort, and then the font will be scaled.  These
  315.  *   resolutions should be in increasing order.
  316.  *
  317.  *   For most machines, just `300' is sufficient here; on the NeXT,
  318.  *   `300 400' may be more appropriate.
  319.  */
  320. case 'R':
  321.          i = 0 ;
  322.          p = was_inline + 1 ;
  323.          while (*p) {
  324.             while (*p && *p <= ' ')
  325.                p++ ;
  326.             if ('0' <= *p && *p <= '9') {
  327.                j = 0 ;
  328.                while ('0' <= *p && *p <= '9')
  329.                   j = 10 * j + (*p++ - '0') ;
  330.                if (i > 0)
  331.                   if (lastresortsizes[i-1] > j) {
  332.                      error("last resort sizes (R) must be sorted") ;
  333.                      bad_config() ;
  334.                   }
  335.                lastresortsizes[i++] = j ;
  336.             } else {
  337.                if (*p == 0)
  338.                   break ;
  339.                error("! only numbers expected on `R' line in config!") ;
  340.             }
  341.          }
  342.          lastresortsizes[i] = 32000 ;
  343.          break ;
  344. case 'D' :
  345.          if (sscanf(was_inline+1, "%d", &actualdpi) != 1) bad_config() ;
  346.          if (actualdpi < 10 || actualdpi > 10000) bad_config() ;
  347.      vactualdpi = actualdpi;
  348.          break ;
  349. /*
  350.  *   Execute a command.  This can be dangerous, but can also be very useful.
  351.  */
  352. case 'E' :
  353. #ifdef SECURE
  354.          error("dvips was compiled with SECURE, which disables E in config") ;
  355. #else
  356.          (void)system(was_inline+1) ;
  357. #endif
  358.          break ;
  359. case 'K':
  360.          removecomments = (was_inline[1] != '0') ;
  361.          break ;
  362. case 'U':
  363.          nosmallchars = (was_inline[1] != '0') ;
  364.          break ;
  365. case 'W':
  366.          for (p=was_inline+1; *p && *p <= ' '; p++) ;
  367.          if (*p)
  368.             warningmsg = newstring(p) ;
  369.          else
  370.             warningmsg = 0 ;
  371.          break ;
  372. case 'X' :
  373.          if (sscanf(was_inline+1, "%d", &actualdpi) != 1) bad_config() ;
  374.          if (actualdpi < 10 || actualdpi > 10000) bad_config() ;
  375.          break ;
  376. case 'Y' :
  377.          if (sscanf(was_inline+1, "%d", &vactualdpi) != 1) bad_config() ;
  378.          if (vactualdpi < 10 || vactualdpi > 10000) bad_config() ;
  379.          break ;
  380. case 'e' :
  381.          if (sscanf(was_inline+1, "%d", &maxdrift) != 1) bad_config() ;
  382.          if (maxdrift < 0) bad_config() ;
  383.      vmaxdrift = maxdrift;
  384.          break ;
  385. case 'q' : case 'Q' :
  386.          quiet = (was_inline[1] != '0') ;
  387.          break ;
  388. case 'f' : case 'F' :
  389.          filter = (was_inline[1] != '0') ;
  390.          break ;
  391. case 'h' : 
  392.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  393.          else (void)add_header(PSname) ;
  394.          break ;
  395. case 'N' :
  396.          disablecomments = (was_inline[1] != '0') ;
  397.          break ;
  398. case 'Z' :
  399.          compressed = (was_inline[1] != '0') ;
  400.          break ;
  401. default:
  402.          bad_config() ;
  403.       }
  404.      }
  405.      (void)fclose(deffile) ;
  406.    } else {
  407.       if (printer)
  408.          error("! no such printer (can't find corresponding config file)") ;
  409.    }
  410. }
  411.  
  412. void getpsinfo() {
  413.    FILE *deffile ;
  414.    register char *p ;
  415.    char *specinfo, *downloadinfo ;
  416.  
  417.    if ((deffile=search(configpath, psmapfile, READ))!=NULL) {
  418.      while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  419.          char *TeXname = NULL ;
  420.          char *PSname = NULL ;
  421.          specinfo = NULL ;
  422.          downloadinfo = NULL ;
  423.          p = was_inline ;
  424.          while (*p) {
  425.             while (*p && *p <= ' ')
  426.                p++ ;
  427.             if (*p) {
  428.                if (*p == '"')
  429.                   specinfo = p + 1 ;
  430.                else if (*p == '<')
  431.                   downloadinfo = p + 1 ;
  432.                else if (TeXname)
  433.                   PSname = p ;
  434.                else
  435.                   TeXname = p ;
  436.                if (*p == '"') {
  437.                   p++ ;
  438.                   while (*p != '"' && *p)
  439.                      p++ ;
  440.                } else
  441.                   while (*p > ' ')
  442.                      p++ ;
  443.                if (*p)
  444.                   *p++ = 0 ;
  445.             }
  446.          }
  447.          if (TeXname) {
  448.             TeXname = newstring(TeXname) ;
  449.             specinfo = newstring(specinfo) ;
  450.             PSname = newstring(PSname) ;
  451.             downloadinfo = newstring(downloadinfo) ;
  452.             add_entry(TeXname, TeXname, PSname, specinfo, downloadinfo) ;
  453.      }
  454.       }
  455.       (void)fclose(deffile) ;
  456.    }
  457. }
  458. /*
  459.  *   Get environment variables! These override entries in ./config.h.
  460.  *   We substitute everything of the form ::, ^: or :$ with default,
  461.  *   so a user can easily build on to the existing paths.
  462.  */
  463. static char *getenvup(who, what)
  464. char *who, *what ;
  465. {
  466.    char *p  ;
  467.  
  468.    if (p=getenv(who)) {
  469.       register char *pp, *qq ;
  470.       int lastsep = 1 ;
  471.  
  472.       for (pp=nextstring, qq=p; *qq;) {
  473.          if (*qq == PATHSEP) {
  474.             if (lastsep) {
  475.                strcpy(pp, what) ;
  476.                pp = pp + strlen(pp) ;
  477.             }
  478.             lastsep = 1 ;
  479.          } else
  480.             lastsep = 0 ;
  481.          *pp++ = *qq++ ;
  482.       }
  483.       if (lastsep) {
  484.          strcpy(pp, what) ;
  485.          pp = pp + strlen(pp) ;
  486.       }
  487.       *pp = 0 ;
  488.       qq = nextstring ;
  489.       nextstring = pp + 1 ;
  490.       return qq ;
  491.    } else
  492.       return what ;
  493. }
  494. void checkenv(which)
  495. int which ;
  496. {
  497.    if (which) {
  498.       tfmpath = getenvup("TEXFONTS", tfmpath) ;
  499.       if (getenv("TEXPKS"))
  500.          pkpath = getenvup("TEXPKS", pkpath) ;
  501.       else if (getenv("PKFONTS"))
  502.          pkpath = getenvup("PKFONTS", pkpath) ;
  503. /* this is not a good idea on most systems.  tgr
  504.       else if (getenv("TEXFONTS"))
  505.          pkpath = getenvup("TEXFONTS", pkpath) ; */
  506.       figpath = getenvup("TEXINPUTS", figpath) ;
  507. #ifdef SEARCH_SUBDIRECTORIES
  508.       if (getenv ("TEXFONTS_SUBDIR"))
  509.          fontsubdirpath = getenvup ("TEXFONTS_SUBDIR", fontsubdirpath);
  510.       {
  511.          char *do_subdir_path();
  512.          static char *concat3();
  513.          char *dirs = do_subdir_path (fontsubdirpath);
  514.          /* If the paths were in dynamic storage before, that memory is
  515.             wasted now.  */
  516.          tfmpath = concat3 (tfmpath, ":", dirs);
  517.          pkpath = concat3 (pkpath, ":", dirs);
  518.       }
  519. #endif
  520.    } else
  521.       configpath = getenvup("TEXCONFIG", configpath) ;
  522. }
  523.  
  524. #ifdef SEARCH_SUBDIRECTORIES
  525.  
  526. #include <sys/types.h>
  527. #include <sys/stat.h>
  528. #include <errno.h>
  529.  
  530. #ifdef SYSV
  531. #include <dirent.h>
  532. typedef struct dirent *directory_entry_type;
  533. #else
  534. #include <sys/dir.h>
  535. typedef struct direct *directory_entry_type;
  536. #endif
  537.  
  538. /* Declare the routine to get the current working directory.  */
  539.  
  540. #ifndef HAVE_GETCWD
  541. extern char *getwd ();
  542. #define getcwd(b, len)  ((b) ? getwd (b) : getwd (xmalloc (len)))
  543. #else
  544. #ifdef ANSI
  545. extern char *getcwd (char *, int);
  546. #else
  547. extern char *getcwd ();
  548. #endif /* not ANSI */
  549. #endif /* not HAVE_GETWD */
  550.  
  551. #ifdef SYSV
  552. #define MAXPATHLEN (256)
  553. #else
  554. #ifdef VMS
  555. #define MAXPATHLEN (256)
  556. #else   /* ~SYSV */
  557. #include <sys/param.h>          /* for MAXPATHLEN */
  558. #endif  /* ~SYSV */
  559. #endif
  560.  
  561. extern void exit(), free() ;
  562. extern int chdir() ;
  563.  
  564. /* Memory operations: variants of malloc(3) and realloc(3) that just
  565.    give up the ghost when they fail.  */
  566.  
  567. extern char *realloc ();
  568.  
  569. char *
  570. xmalloc (size)
  571.   unsigned size;
  572. {
  573.   char *mem = malloc (size);
  574.   
  575.   if (mem == NULL)
  576.     {
  577.       fprintf (stderr, "! Cannot allocate %u bytes.\n", size);
  578.       exit (10);
  579.     }
  580.   
  581.   return mem;
  582. }
  583.  
  584.  
  585. char *
  586. xrealloc (ptr, size)
  587.   char *ptr;
  588.   unsigned size;
  589. {
  590.   char *mem = realloc (ptr, size);
  591.   
  592.   if (mem == NULL)
  593.     {
  594.       fprintf (stderr, "! Cannot reallocate %u bytes at %x.\n", size, ptr);
  595.       exit (10);
  596.     }
  597.     
  598.   return mem;
  599. }
  600.  
  601.  
  602. /* Return, in NAME, the next component of PATH, i.e., the characters up
  603.    to the next PATHSEP.  */
  604.    
  605. static void
  606. next_component (name, path)
  607.   char name[];
  608.   char **path;
  609. {
  610.   unsigned count = 0;
  611.   
  612.   while (**path != 0 && **path != PATHSEP)
  613.     {
  614.       name[count++] = **path;
  615.       (*path)++; /* Move further along, even between calls.  */
  616.     }
  617.   
  618.   name[count] = 0;
  619.   if (**path == PATHSEP)
  620.     (*path)++; /* Move past the delimiter.  */
  621. }
  622.  
  623.  
  624. #ifndef _POSIX_SOURCE
  625. #define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
  626. #endif
  627.  
  628. /* Return true if FN is a directory or a symlink to a directory,
  629.    false if not. */
  630.  
  631. int
  632. is_dir (fn)
  633.   char *fn;
  634. {
  635.   struct stat stats;
  636.  
  637.   return stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode);
  638. }
  639.  
  640.  
  641. static char *
  642. concat3 (s1, s2, s3)
  643.   char *s1, *s2, *s3;
  644. {
  645.   char *r = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1);
  646.   strcpy (r, s1);
  647.   strcat (r, s2);
  648.   strcat (r, s3);
  649.   return r;
  650. }
  651.  
  652.  
  653. /* DIR_LIST is the default list of directories (colon-separated) to
  654.    search.  We want to add all the subdirectories directly below each of
  655.    the directories in the path.
  656.      
  657.    We return the list of directories found.  */
  658.  
  659. char *
  660. do_subdir_path (dir_list)
  661.   char *dir_list;
  662. {
  663.   char *cwd;
  664.   unsigned len;
  665.   char *result = xmalloc (1);
  666.   char *temp = dir_list;
  667.  
  668.   /* Make a copy in writable memory.  */
  669.   dir_list = xmalloc (strlen (temp) + 1);
  670.   strcpy (dir_list, temp);
  671.   
  672.   *result = 0;
  673.  
  674.   /* Unfortunately, we can't look in the environment for the current
  675.      directory, because if we are running under a program (let's say
  676.      Emacs), the PWD variable might have been set by Emacs' parent
  677.      to the current directory at the time Emacs was invoked.  This
  678.      is not necessarily the same directory the user expects to be
  679.      in.  So, we must always call getcwd(3) or getwd(3), even though
  680.      they are slow and prone to hang in networked installations.  */
  681.   cwd = getcwd (NULL, MAXPATHLEN + 2);
  682.   if (cwd == NULL)
  683.     {
  684.       perror ("getcwd");
  685.       exit (errno);
  686.     }
  687.  
  688.   do
  689.     {
  690.       DIR *dir;
  691.       directory_entry_type e;
  692.       char dirname[MAXPATHLEN];
  693.  
  694.       next_component (dirname, &dir_list);
  695.  
  696.       /* All the `::'s should be gone by now, but we may as well make
  697.          sure `chdir' doesn't crash.  */
  698.       if (*dirname == 0) continue;
  699.  
  700.       /* By changing directories, we save a bunch of string
  701.          concatenations (and make the pathnames the kernel looks up
  702.          shorter).  */
  703.       if (chdir (dirname) != 0) continue;
  704.  
  705.       dir = opendir (".");
  706.       if (dir == NULL) continue;
  707.  
  708.       while ((e = readdir (dir)) != NULL)
  709.         {
  710.           if (is_dir (e->d_name) && strcmp (e->d_name, ".") != 0
  711.               && strcmp (e->d_name, "..") != 0)
  712.             {
  713.               char *found = concat3 (dirname, "/", e->d_name);
  714.  
  715.               result = xrealloc (result, strlen (result) + strlen (found) + 2);
  716.  
  717.               len = strlen (result);
  718.               if (len > 0)
  719.                 {
  720.                   result[len] = PATHSEP;
  721.                   result[len + 1] = 0;
  722.                 }
  723.               strcat (result, found);
  724.               free (found);
  725.             }
  726.         }
  727.       closedir (dir);
  728.  
  729.       /* Change back to the current directory, in case the path
  730.          contains relative directory names.  */
  731.       if (chdir (cwd) != 0)
  732.         {
  733.           perror (cwd);
  734.           exit (errno);
  735.         }
  736.     }
  737.   while (*dir_list != 0);
  738.   
  739.   return result;
  740. }
  741. #endif /* SEARCH_SUBDIRECTORIES */
  742.